package com.tgb.web;  
  
import java.io.IOException;  
import java.io.PrintWriter;  
  
 

import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
  


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
  


import com.tgb.entity.User;  
import com.tgb.manager.UserManager;  
  
@Controller  
@RequestMapping("/user")  
public class UserController {  
  
    //@Resource(name="userManager")  
	@Autowired
    private UserManager userManager;  
  
    @RequestMapping("/welcome")
    public String welcome(){
    	return "welcome";   	
    }
    
   /* @RequestMapping("/getAllUser")  
    public String getAllUser(HttpServletRequest request){  
          
        request.setAttribute("userList", userManager.getAllUser());  
          
        return "index";  
      
    }  */
        
    @RequestMapping("/getUser")  
    public String getUser(String teacherId,String userId,HttpServletRequest request){  
          
    	request.setAttribute("teacherId",teacherId);
        request.setAttribute("user", userManager.getUser(userId));  
      
        return "editUser";  
    }  
      
    @RequestMapping("/toAddUser")  
    public String toAddUser(){  
        return "addUser";  
    }  
      
    @RequestMapping("/addUser")  
    public String addUser(String teacherId,User user,HttpServletRequest request){  
          
        userManager.addUser(teacherId,user);  
          
        return "redirect:/main/list";  
    }  
      
    @RequestMapping("/delUser")  
    public void delUser(String id,HttpServletResponse response){  
          
        String result = "{\"result\":\"error\"}";  
          
        if(userManager.delUser(id)){  
            result = "{\"result\":\"success\"}";  
        }  
          
        response.setContentType("application/json");  
          
        try {  
            PrintWriter out = response.getWriter();  
            out.write(result);  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
      
    @RequestMapping("/updateUser")  
    public String updateUser(User user,HttpServletRequest request){  
          
        if(userManager.updateUser(user)){  
            user = userManager.getUser(user.getId());  
            request.setAttribute("user", user);  
            return "redirect:/main/list";  
        }else{  
            return "/error";  
        }  
    }  
}  
